package com.cskt.petproject178.service.impl;

import com.cskt.petproject178.mapper.SkuMapper;
import com.cskt.petproject178.mapper.SpuMapper;
import com.cskt.petproject178.pojo.exception.BaseException;
import com.cskt.petproject178.pojo.vo.ShoppingCartVO;
import com.cskt.petproject178.pojo.vo.StatusEnum;
import com.cskt.petproject178.pojo.vo.UserInfoVO;
import com.cskt.petproject178.service.ShoppingCartService;
import com.cskt.petproject178.service.third.AliyunOSSService;
import com.cskt.petproject178.service.third.AliyunSMSService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 27013
 **/

@Service
public class ShoppingCartServiceImpl  implements ShoppingCartService {
    private static final Logger log = LoggerFactory.getLogger(ShoppingCartServiceImpl.class);
    @Resource
    private SpuMapper spuMapper;
    @Resource
    private ObjectMapper objectMapper;
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Resource
    private AliyunOSSService aliyunOSSService;
    @Override
    public Boolean getSpuProductBySkuId(Long skuId,Long userId,Integer num) {

        try {
            //必须判断该用户的购物车商品是否已经存在，如果存在则更新数量和总价，如果不存在则直接添加到购物车中
        String cartKey = "pet:cart:" + userId;
       String shoppingCartStr=(String) stringRedisTemplate.opsForHash().get(cartKey,skuId.toString());
        if (StringUtils.hasText(shoppingCartStr)) {
            //购物车中已经有对应的商品
                ShoppingCartVO shoppingCartVO = objectMapper.readValue(shoppingCartStr, ShoppingCartVO.class);
              int newNum=shoppingCartVO.getNum() + num;
                      shoppingCartVO.setNum(newNum);
                shoppingCartVO.setPrice(shoppingCartVO.getUnitPrice().multiply(new BigDecimal(newNum)));
                //更新购物车
                stringRedisTemplate.opsForHash().put(cartKey,skuId.toString(),objectMapper.writeValueAsString(shoppingCartVO));
        }else {
            //查询商品进行添加
            ShoppingCartVO shopping = spuMapper.getSpuProductBySkuId(skuId);
            if (ObjectUtils.isEmpty(shopping)) {
                log.warn("查询购物车商品失败{}", skuId);
                return false;
            }
            shopping.setNum(num);
            shopping.setAddTime(LocalDateTime.now());
            shopping.setPrice(shopping.getUnitPrice().multiply(BigDecimal.valueOf(num)));
            shopping.setPicUrl(aliyunOSSService.getObjUrl(shopping.getPicUrl()));
            //拿到用户编号进行set存入
            shopping.setUserId(userId);
            //将对象转换成json字符串存入redis中,使用hash类型

            stringRedisTemplate.opsForHash().put(cartKey, String.valueOf(skuId), objectMapper.writeValueAsString(shopping));
        }
            return true;
        } catch (Exception e) {
            log.error("添加购物车时序列化失败",e);
            throw new BaseException(StatusEnum.COMMON_EXCEPTION);
        }
    }
}
